import java.util.*;
/*
Write a function to find the longest common prefix string amongst an array of strings.
[]
=>""
["abcweed","htgdabc","sabcrf"]
=>""
["abcweed","abhtgdc","abacrf"]
=>"ab"
*/
public class Solution{
	public String longestCommonPrefix(String[] strs) {
		//System.out.println(Arrays.toString(strs));
		if(strs.length == 0)
			return "";
		int min = Integer.MAX_VALUE;
		for(String s : strs){
			if(min>s.length())
				min = s.length();
		}
		if(min==0)
			return "";
		//System.out.println(min);
		String prefix = "", tmp = "";
		int i=0;
		for( ; i<min; i++){
			prefix = strs[0].substring(0,(i+1));
			//System.out.println("prefix=" + prefix);
			for(int j=1; j<strs.length; j++){
				tmp = strs[j].substring(0,(i+1));
				//System.out.println("tmp=" + tmp);
				if(!prefix.equals(tmp))
					return strs[0].substring(0,(i));
			}
		}
		System.out.println("i=" + strs[0].substring(0,(i)));
		return "";
	}
	
	
	public static void main(String[] args){
		String[] arr = {"","asdffg","aswd"};
		if(args.length!=0)
			arr = args;
		Solution solution = new Solution();
		String res = solution.longestCommonPrefix(arr);
		System.out.println(res);
	}
}
/*
if(strs.length==0)
	return "";
int min = strs[0].length();
char[][] arr = new char[strs.length][];
for(int i=0; i<strs.length; i++){
	arr[i] = strs[i].toCharArray();
	if(strs[i].length()==0)
		return "";
	if(min>strs[i].length())
		min = strs[i].length();
}
int res = getPrefix(arr, min);
return strs[0].substring(0,res);

public int getPrefix(char[][] arr,int min){
	char prefix = '0';
	for(int i=0; i<min; i++){
		for(int j=0; j<arr.length; j++){
			if(j==0){
				prefix = arr[j][i];
				continue;
			}
			if(arr[j][i] != prefix){
				return i;
			}
		}
	}
	return min+1;
}
*/